package com.admore.demo.draw;

import static com.admore.sdk.config.AdMoreConstants.AD_HEIGHT;
import static com.admore.sdk.config.AdMoreConstants.AD_WIDTH;

import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.admore.demo.App;
import com.admore.demo.R;
import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreDrawAd;
import com.admore.sdk.ads.DrawAd;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.callback.AdMoreRenderListener;
import com.admore.sdk.config.AdMoreInfo;
import com.admore.sdk.sdk.AdMoreSdk;
import com.zcy.base.ui.BaseActivity;

import java.util.HashMap;
import java.util.List;

public class AdMoreDrawActivity extends BaseActivity {

    private FrameLayout adMain;
    private AdMoreDrawAd adMoreDrawAd;

    @Override
    public int bindLayout() {
        return R.layout.activity_draw;
    }

    @Override
    public void initView() {
        adMain = findViewById(R.id.ad_main);
        ((TextView) findViewById(R.id.title)).setText("draw");
        ((TextView) findViewById(R.id.title)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adMoreDrawAd == null) {
                    startDrawAd();
                } else {
                    if (adMoreDrawAd.getDrawAds().size() > 0) {
                        showDrawAd();
                    } else {
                        adMoreDrawAd.loadAd();
                    }
                }
            }
        });

    }

    @Override
    public void initData() {

        startDrawAd();
    }

    private void startDrawAd() {
        if (!AdMoreSdk.init) {
            return;
        }
        HashMap<String, Object> options = new HashMap<>();
        options.put(AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
        options.put(AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
        adMoreDrawAd = AdMoreSdk.getAdMoreSdkManager().createDraw(this, App.AD_MORE_DRAW_ID, options);
        adMoreDrawAd.setLoadListener(new AdMoreLoadListener() {
            @Override
            public void onAdLoadSuccess(String msg) {
                Log.d(TAG, "onAdLoadSuccess");
                showDrawAd();
            }

            @Override
            public void onAdLoadError(Throwable throwable) {
                Log.d(TAG, "onAdLoadError " + throwable);

            }
        });
        adMoreDrawAd.loadAd();
    }


    private void showDrawAd() {
        if (adMoreDrawAd == null || adMoreDrawAd.getDrawAds() == null || adMoreDrawAd.getDrawAds().size() == 0) {
            Toast.makeText(getApplicationContext(), "请先加载广告！", Toast.LENGTH_SHORT).show();
            return;
        }
        final DrawAd drawAd = adMoreDrawAd.getDrawAds().remove(0);
        AdMoreInfo adMoreInfo = drawAd.getAdMoreInfo();
        drawAd.setAdMoreRenderListener(new AdMoreRenderListener() {
            @Override
            public void onRenderSuccess() {
                Log.d(TAG, "onRenderSuccess");
            }

            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG, "onRenderError " + throwable);

            }
        });
        drawAd.setMediaListener(new AdMoreMediaListener() {
            @Override
            public void onAdClosed() {
                Log.d(TAG, "onAdClosed");
            }

            @Override
            public void onAdShow() {
                Log.d(TAG, "onAdShow ecpm:" + drawAd.getAdMoreInfo().getECPM() + " adName:" + drawAd.getAdMoreInfo().getSdkName());
            }
        });
        drawAd.render(this, adMain);
    }
}
